package cn.wolfcode.luowowo.comment.service.impl;


import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by LENOVO on 2019/8/13.
 */

/**
 *
 *
 *
 *
 */
@Service
public class TravelCommentServiceImpl implements ITravelCommentService {


    @Autowired
    private ITravelCommentRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public TravelComment saveOrUpdate(TravelComment comment) {
        //判断文档的主键id是否空，如果为空就设置创建时间，否则不设置
        if (!StringUtils.hasLength(comment.getId())) {
            //创建
            comment.setCreateTime(new Date());
        }
        //评论的评论
        //如果有就设置进去
        if (comment.getType() == TravelComment.TRAVLE_COMMENT_TYPE) {
            Optional<TravelComment> op = repository.findById(comment.getRefComment().getId());
            comment.setRefComment(op.get());
        }
        repository.save(comment);
        return comment;

    }

    @Override
    public Page query(TravelCommentQuery qo) {
        //查询条件
        Query query = new Query();
        if (qo.getTravelId() != -1) {
            //攻略id条件
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        //查总数
        long count = template.count(query, TravelComment.class);

        if (count == 0) {
            return Page.empty();
        }
        //select count(id) from xxx where ....
        //select * from xxx where .... order by
        //参数1:当前页, 注意, 从0开始
        //参数2:每页显示条数
        //参数3:排序规则, 参数a: 表示倒序  参数b:哪个字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);

        //查某一页  List
        List<TravelComment> list = template.find(query, TravelComment.class);
        // PageImpl  Page接口实现类
        return new PageImpl(list, pageable, count);


    }

    @Override
    public List<TravelComment> getNumTop10() {
        //repository.findAll(PageRequest.of(commentQuery.getCurrentPage(),commentQuery.getPageSize(),Sort.by(Sort.Direction.DESC,"createTime")));

        Query query = new Query();
        query.limit(10);
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        List<TravelComment> travelComments = template.find(query, TravelComment.class);
        return travelComments;
    }


}
